package com.jic.manager.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.MestoreSupplier;
import com.jic.manager.mapper.MestoreSupplierMapper;
import com.jic.manager.service.MestoreSupplierService;
import com.jic.util.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 供应商店铺
 * @author : 
 * @email : 
 * @since : 2020-02-26 09:39:51
 * @version : v1.0.0
 */
@Slf4j
@Service
public class MestoreSupplierServiceImpl implements MestoreSupplierService{

    @Resource
    private MestoreSupplierMapper mestoreSupplierMapper;

    @Override
    public long insert(MestoreSupplier mestoreSupplier) {
        long flag = mestoreSupplierMapper.insert(mestoreSupplier);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return mestoreSupplierMapper.delete(id);
    }

    @Override
    public long update(MestoreSupplier mestoreSupplier) {
        return mestoreSupplierMapper.update(mestoreSupplier);
    }

    @Override
    public MestoreSupplier queryByPrimaryKey(Long id){
        return mestoreSupplierMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MestoreSupplier> query(MestoreSupplier mestoreSupplier){
        return mestoreSupplierMapper.query(mestoreSupplier);
    }

    @Override
    public PageResult<MestoreSupplier> queryPage(MestoreSupplier mestoreSupplier, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<MestoreSupplier> list = mestoreSupplierMapper.queryPage(startIndex, pageQuery.getLimit(), mestoreSupplier);
        long count = mestoreSupplierMapper.queryPageCount(mestoreSupplier);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<String> saveSupplierInfo(MestoreSupplier memStoreSupplier) {
        try{
            Long id =memStoreSupplier.getId();
            // TODO
            if(id !=null && id!=0){
                mestoreSupplierMapper.update(memStoreSupplier);
            }else {
                memStoreSupplier.setCreateTime(new Date());
                memStoreSupplier.setDeleteFlag(0);
                memStoreSupplier.setStatus(Integer.valueOf("0"));
                mestoreSupplierMapper.insert(memStoreSupplier);
            }
            return RestResult.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return RestResult.error("9999",e.getLocalizedMessage());
        }
    }

    @Override
    public RestResult<String> delSupplierInfo(Long id) {
        mestoreSupplierMapper.update(new MestoreSupplier(){{
           setId(id);
           setDeleteFlag(0);
        }});
        return RestResult.success("成功");
    }

    @Override
    public RestResult<String> upSupplierStatus(MestoreSupplier mestoreSupplier) {
        try{
            Long id =mestoreSupplier.getId();
            if(id != null && id!=0){
                mestoreSupplierMapper.update(mestoreSupplier);
//                MestoreSupplier memStoreSuppliers=mestoreSupplierMapper.queryByPrimaryKey(id);
//                if(memStoreSuppliers.getStatus()==0){
//                    memStoreSuppliers.getId();
//                    memStoreSuppliers.setStatus(0);
//                    mestoreSupplierMapper.update(memStoreSuppliers);
//                }else {
//                    memStoreSuppliers.setStatus(0);
//                    mestoreSupplierMapper.update(memStoreSuppliers);
//                }
            }
            return RestResult.success("成功");
        }catch (Exception e){
            // TODO
            e.printStackTrace();
            return RestResult.error("9999",e.getLocalizedMessage());
        }
    }

    // 查询供应商详情
    @Override
    public RestResult<MestoreSupplier> getSupplierInfo(MestoreSupplier mestoreSupplier) {
        RestResult result = new RestResult();
          Long id = mestoreSupplier.getId();
          if(id != null && id != 0){
                MestoreSupplier mestoreSupplier1= mestoreSupplierMapper.queryByPrimaryKey(id);
                result.setData(mestoreSupplier1);
          }else {
              log.error("id为空，请查询前台传入参数");
          }
          return  result;
    }



}